import Http from './request.js';
import {
	domain,
	domainHttp
} from '@/utils/config.js'
import {
	Toast
} from './utils.js';
import {
	useMainStore
} from '@/stores/index';
const mainStore = useMainStore();

export const getopendId = () => {
	// #ifdef MP-WEIXIN || APP
	uni.getProvider({
		service: 'oauth',
		success: (res) => {
			uni_Login(res.provider[0]);
		}
	})
	// #endif
	// #ifdef H5
	getBaseInfo();
	myInfo();
	// #endif
}

export const uni_Login = (provider) => {
	uni.login({
		provider,
		success: (req) => {
			const requestData = {
				code: req.code
			}
			Http.post({
				url: '/api/wxLogin',
				data: requestData
			}).then(res => {
				if (res.code != 0) return Toast(res.msg);
				if (res.open_id) mainStore.openid = res.open_id;
				if (res.session_key) mainStore.session_key = res.session_key;
				mainStore.token = res.token;
				getBaseInfo();
				if (mainStore.token) myInfo();

			})
		}
	})


}

export const getBaseInfo = () => {
	Http.get({
		url: '/api/getBaseInfo'
	}).then(res => {
		if (res.code != 0) return Toast(res.msg);
		mainStore.BaseInfo = res.data;
	})
}

export const myInfo = () => {
	Http.get({
		url: '/api/myInfo',
	}).then(res => {
		if (res.code != 0) return Toast(res.msg);
		const result = res.data;
		mainStore.$patch({
			userInfo: result,
			portType: result.portType,
			employeeType: result.employeeType
		})

	})
}

// 律师
export const lawyerInfo = () => {
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/lawyer/info',
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res);
		})
	})
}

// 选择律师所
export const lawFirmSelectList = (lawFirmId) => {
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/lawyer/lawFirmSelectList',
			data: {
				lawFirmId
			}
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res.data || []);
		})
	})
}

// 律师领域
export const professionField = ()=>{
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/lawyer/professionField',
		
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res.data || []);
		})
	})
}

// 律师审核状态

export const getLawyerStatus = ()=>{
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/lawyer/getLawyerStatus',
		
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res.data);
		})
	})
}

export const saveMyInfo = (paramData = {}) => {
	Http.post({
		url: '/api/saveMyInfo',
		data: paramData
	}).then(res => {
		if (res.code != 0) return Toast(res.msg);
		myInfo();
	})
}


// 律师类型
export const getByType = (type) => {
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/dict/getByType',
			data: {
				type
			}
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res.data)
		})
	})
}


// 发起签约上传合同
export const uploadStreamFile = (Result) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: domain + '/api/tripartite/msca/uploadStreamFile',
			filePath: Result.path,
			name: 'file',
			dataType: "json",
			formData: {
				file: Result.path,
				filename: Result.name,
				suffix: Result.name ? Result.name.split('.')[1] : ''
			},
			header: {
				'token': mainStore.token
			},
			success: (uploadFileRes) => {
				resolve(JSON.parse(uploadFileRes.data))
			},
			fail: (err) => {
				console.log(err);
			},
			complete: (complete) => {
				console.log(complete);
			}
		})
	})
}

// 发起合同
export const pactInit = (orderId, signFiles) => {
	return new Promise((resolve, reject) => {
		Http.post({
			url: '/order/pactInit',
			data: {
				orderId,
				signFiles
			}
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res)
		})
	})
}

// 获取对方用户openid信息
export const getUserByOrderId = (orderId) => {
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/order/getUserByOrderId',
			data: {
				orderId
			}
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);

			resolve(res.data)
		})
	})
}


// 获取签约状态
export const getOrderPactStatus = (orderId) => {
	return new Promise((resolve, reject) => {
		Http.get({
			url: '/order/getOrderPactStatus',
			data: {
				orderId
			}
		}).then(res => {
			if (res.code != 0) return Toast(res.msg);
			resolve(res.data)
		})
	})
}